import time
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By

dr = webdriver.Chrome()
dr.get("https://movie.douban.com/tv/?support_type=tv&is_all=false&category=tv&type=tv")
dr.maximize_window()
time.sleep(3)
# 点击查看更多
dr.find_element(By.CSS_SELECTOR,".subject-list-more button").click()
time.sleep(2)
# 找到所有的电视剧
es = dr.find_elements(By.CSS_SELECTOR,".subject-list-list li")
titles = []
urls = []
for e in es:
    pic = e.find_element(By.CSS_SELECTOR,".drc-cover-pic")
    # 根据图片元素 取得对应的地址
    url = pic.get_attribute("src")
    # 使用requests库进行请求
    # response = requests.get(url)
    title = e.find_element(By.CSS_SELECTOR,".drc-subject-info-title-text").text
    # with open("电视剧/"+title+".png","wb+") as f:
    #     f.write(response.content)
    urls.append(url)
    titles.append(title)
# 定义一个函数 进行下载使用
def save_file(title,url):
    response = requests.get(url)
    with open("电视剧/"+title+".png","wb+") as f:
        f.write(response.content)
# 使用线程池进下载
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor(max_workers=10) as pool:
    pool.map(save_file,titles,urls)